package com.netsdk.lht2.handle.support;

import com.netsdk.demo.util.DateUtil;
import com.netsdk.demo.util.sql.EventDto;
import com.netsdk.lht2.handle.EventHandle;
import com.netsdk.lib.ToolKits;
import com.netsdk.lib.structure.DEV_EVENT_TRAFFICACCIDENT_INFO;
import com.netsdk.lib.structure.NET_TIME_EX;
import com.sun.jna.Pointer;
import org.apache.commons.lang3.StringUtils;

import java.io.UnsupportedEncodingException;
import java.util.Date;

/*
*  交通事故事件处理
* */
public class EVENT_IVS_TRAFFICACCIDENT_HANDLE extends EventHandle {

    @Override
    public void handle(int dwAlarmType, Pointer pAlarmInfo, Pointer pBuffer, int dwBufSize, Pointer dwUser, int nSequence, Pointer reserved) throws UnsupportedEncodingException {
        System.out.println("交通事故事件, code = " + dwAlarmType);
        DEV_EVENT_TRAFFICACCIDENT_INFO msg = new DEV_EVENT_TRAFFICACCIDENT_INFO();
        ToolKits.GetPointerData(pAlarmInfo, msg);
        // 通道号
        EventDto dto = new EventDto();
        String nChannelID = getChannelMapping(msg.nChannelID);
        System.out.println("nChannelID:" + msg.nChannelID);
        // 动作
//        int nAction = msg.nAction;
//        System.out.println("nAction:" + nAction);
        // 车道号
//        int nLaneID = msg.nLaneID;
//        System.out.println("nLaneID:" + nLaneID);
        // 抓拍过程
//        int emCaptureProcess = msg.emCaptureProcess;
//        System.out.println("emCaptureProcess:" + emCaptureProcess + "," + EM_CAPTURE_PROCESS_END_TYPE.getNoteByValue(emCaptureProcess));
        //事件名称
        byte[] szName = msg.szName;
        System.out.println("szName UTF-8:" + new String(szName,"UTF-8").trim());
        System.out.println("szName GBK:" + new String(szName,"GBK").trim());
        //相对事件时间戳(单位是毫秒)
        double pts = msg.PTS;
        System.out.println("pts:" + new Date((long) pts));
        //UTC时间
        NET_TIME_EX UTC = msg.UTC;
        System.out.println("UTC:" + UTC.toStringTime());
        //事件id
        int nEventID = msg.nEventID;
        System.out.println("nEventID:" + nEventID);

        //图片保存
        if (pBuffer != null && dwBufSize > 0 && msg.stuFileInfo.bCount == 1) {
            String pName = System.currentTimeMillis() + "related_TrafficAccident_common.jpg";
            String bigPicture = picturePath + "\\" + pName;
            dto.setBigPicture(StringUtils.isNotEmpty(dto.getBigPicture()) ? dto.getBigPicture() + ","+pName : ""+pName );
            dto.setImg_name(dto.getBigPicture());
            ToolKits.savePicture(pBuffer, 0, dwBufSize, bigPicture);
        }
        dto.setEvent_content(msg.stuFileInfo.toString());
        dto.setEvent_name("交通事故事件");
        dto.setEvent_code(dwAlarmType+"");
        dto.setChannel(nChannelID);
//        dto.setnAction(nAction+"");
//        dto.setLane(nLaneID+"");
        dto.setEvent_utc_time(UTC.toStringTime());
//        dto.setEmCaptureProcess(emCaptureProcess+"");
        dto.setSzName(new String(szName,"UTF-8").trim());
        dto.setEvent_time(DateUtil.format(new Date((long) pts), "yyyy-MM-dd HH:mm:ss.SSS"));
        dto.setEvent_id(nEventID+"");
        // 数据入库
        insertEventDto(dto);
    }
}
